package vn.lumi.smarthome.ui;

import vn.lumi.smarthome.io.database.TBL_Device;
import vn.lumi.smarthome.io.database.TBL_DeviceState;
import vn.lumi.smarthome.io.model.Device;
import vn.lumi.smarthome.io.model.DeviceState;
import vn.lumi.smarthome.ulti.LogUltis;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;

public abstract class MyCursorAdapter extends CursorAdapter{
	protected String TAG="MyCursorAdapter";
	public MyCursorAdapter(Context context, Cursor c, int flags) {
		super(context, c, flags);
	}

	/**
	 * Lay ra device tu Cursor
	 * @param cursor
	 * @return
	 */
	protected Device getDevice(Cursor cursor){
		int device_id=cursor.getInt(cursor.getColumnIndex(TBL_Device._ID));
		int device_type=cursor.getInt(cursor.getColumnIndex(TBL_Device.TYPE_ID));
		int room_id=cursor.getInt(cursor.getColumnIndex(TBL_Device.ROOM_ID));
		String device_name=cursor.getString(cursor.getColumnIndex(TBL_Device.NAME));
		int device_stateid=cursor.getInt(cursor.getColumnIndex(TBL_Device.STATE_ID));
		boolean isControl=false;
		int control=cursor.getInt(cursor.getColumnIndex(TBL_Device.IS_CONTROL));
		if(control==1)isControl=true;
		
//		int state_id=cursor.getInt(cursor.getColumnIndex(TBL_DeviceState._ID));
		int value=cursor.getInt(cursor.getColumnIndex(TBL_DeviceState.VALUE));
//		int RGB=cursor.getInt(cursor.getColumnIndex(TBL_DeviceState.RGB));
		
		LogUltis.i(TAG, "device_name="+device_name);
		LogUltis.i(TAG, "iscontrol="+control);
		LogUltis.i(TAG, "device_id="+device_id);
		LogUltis.i(TAG, "device_type="+device_type);
		LogUltis.i(TAG, "room_id="+room_id);
		LogUltis.i(TAG, "device_STATEid="+device_stateid);
		LogUltis.i(TAG, "device_value="+value);
		LogUltis.i(TAG, "==============================");
		
		DeviceState deviceState=new DeviceState(device_stateid, value, 0,0);
		return new Device(device_id, device_type, room_id, device_name, device_stateid, deviceState,isControl);
	}
	
	/**
	 * Lay' ra loai device tu Cursor
	 * @param cursor
	 * @return
	 */
	protected int getItemViewType(Cursor cursor){
		return Device.getLayoutType(cursor.getInt(cursor.getColumnIndex(TBL_Device.TYPE_ID)));
	}
	
	@Override
	public int getItemViewType(int position) {
		Cursor cursor=(Cursor)getItem(position);
		return getItemViewType(cursor);
	}
	@Override
	public int getViewTypeCount() {
		return Device.NUM_LAYOUT_TYPE;
	}
}
